// pages/carriage/carriage.js
var util = require('../../utils/util.js');
const app = getApp()
Page({
  /**
   * 页面的初始数据
   */
  data: {
    tabArr: {
      curHdIndex: 0,
      curBdIndex: 0
    },
    navindex: 1,
    // carriage: [
    //   { name: '闪闪', phone: '13230057427' },
    //   { name: '闪闪', phone: '13261051717' },
    //   { name: '闪闪', phone: '13214602964' },
    //   { name: '闪闪', phone: '13248512619' },
    //   { name: '闪闪', phone: '13240104185' },
    // ],
    chose: false,
    edit: '编辑',
    currentPage: 1,
    totalPage: 1,
    carriage:[],
    issel:0
  },
  //遍历添加样式
  eachCheck: function () {
    let tempCarier = app.globalData.carrierData;
    let tempStr = tempCarier.idStr;
    let tempArr = '';
    if (!tempStr){
      return;
    }
    tempArr = tempStr.split(",");
    let data = this.data.carriage;
    for (let i = 0; i < tempArr.length; i++) {
      for (let j = 0; j < data.length; j++) {
        if (data[j]['user_info']['id'] == tempArr[i]) {
          data[j]['ischeck'] = 1;
        }
      }
    }
    this.setData({
      carriage: data
    })
  },
  // bindtap='selCarrier' 
  initCarrierList: function () {
    var that = this;
    var param = {
      currentPage: that.data.currentPage
    };
    wx.showLoading({})
    util.commonRequest('carrierList', 'post', param, function (res) {
      wx.hideLoading()
      if (res.data.code == 200) {
        var tempArr = res.data.data.data;
        for(var i =0; i < tempArr.length; i++){
          tempArr[i]['ischeck'] = false;
        }
        var oriArr = that.data.carriage;
        for (let i = 0; i < oriArr; i++)
        {
          oriArr[i]['ischeck'] = 0;
        }
        var finalyArr = oriArr.concat(tempArr);
       
        that.setData({
          carriage: finalyArr,
          totalPage: res.data.data.last_page
        })
        that.eachCheck();
      } else {
        wx.showToast({
          icon: 'none',
          title: data.msg,
        })
      }
    });
  },

  selCarrier:function(e){
    let index = e.currentTarget.dataset.index;
    let isFree = e.currentTarget.dataset.isfree;
    // debugger
    if (isFree == 0){
      let tempArr = this.data.carriage;
      tempArr[index]['ischeck'] = !tempArr[index]['ischeck'];
      this.setData({
        carriage: tempArr
      })
    } else{
      wx.showToast({
        icon:'none',
        title: '司机正在接单中',
      })
    }

    
    // var id = e.currentTarget.dataset.id;
    // var name = e.currentTarget.dataset.name;
    // if(this.data.issel == 1)
    // {
    //   wx.setStorageSync("zdCarrierId", id);
    //   wx.setStorageSync("zdCarrierName", name);
    //   wx.navigateBack({})
    // }
  },
  confirmSave:function(){
    let data = this.data.carriage;
    let str = '';
    let strTp = '';
    for(let i = 0; i < data.length; i++)
    {
      if (data[i]['ischeck'] == 1){
        str += data[i]['user_info']['id']+',';
        strTp += data[i]['user_info']['name']+',';
      }
    }
    str = str.substr(0, str.length - 1);
    strTp = strTp.substr(0, strTp.length - 1);
    let tempArr = {
      idStr:str,
      nameStr:strTp
    };
    // wx.setStorageSync("carrierData", tempArr);

    app.globalData.carrierData = tempArr;
    wx.navigateBack({})
  },
  //点击添加承运人
  submit(e) {
    var phone = e.detail.value.phone;//获取电话值
    if (!phone) {
      wx.showToast({
        title: '电话不能为空',
        icon: 'none',
      })
      return false;
    } if (!(/^1[3|4|5|8][0-9]\d{4,8}$/.test(phone))) {
      wx.showToast({
        title: '请输入正确的电话',
        icon: 'none',
      })
      return false;
    }

    var that = this;;
    var param = e.detail.value;
    wx.showLoading({})
    util.commonRequest('addcarrier', 'post', param, function (res) {
      wx.hideLoading()
      if (res.data.code == 200) {
        wx.showToast({
          title: '添加成功',
          icon: 'success',
          duration: 1500
        })
        that.setData({
          currentPage: 1,
          totalPage: 1,
          carriage: []
        })
        that.initCarrierList();
      } else {
        wx.showToast({
          icon: 'none',
          title: res.data.msg,
        })
      }
    });

  },
  tabFun: function (e) {
    //获取触发事件组件的dataset属性 
    var _datasetId = e.target.dataset.id;
    console.log("----" + _datasetId + "----");
    var _obj = {};
    _obj.curHdIndex = _datasetId;
    _obj.curBdIndex = _datasetId;
    this.setData({
      tabArr: _obj
    });
  },
  // 点编辑删除
  start_edit() {
    var chose = this.data.chose;
    var edit = this.data.edit;
    console.log(chose)
    if (edit == '编辑') {
      this.setData({
        chose: true,
        edit: '完成'
      })
    } else {
      this.setData({
        chose: false,
        edit: '编辑'
      })
    }
  },
  //删除
  dalete(e) {
    var that = this;
    var id = e.currentTarget.dataset.id;
    wx.showModal({
      title: '温馨提示:',
      content: '您确定删除该承运人?删除后无法恢复',
      success: function (res) {
        if (res.cancel) {
          //点击取消,默认隐藏弹框
        } else {
          that.deleteFun(id);
        }
      },
    })
   
  },
  deleteFun: function (id){
    var that = this;
    var that = this;
    wx.showLoading({})
        
    util.commonRequest('delcarrier', 'post', { idArr: id }, function (res) {
      wx.hideLoading()
      if (res.data.code == 200) {
        wx.showToast({
          title: '删除成功',
          icon: 'success',
          duration: 1500
        })
        that.setData({
          currentPage: 1,
          totalPage: 1,
          carriage: []
        })
        that.initCarrierList();
      } else {
        wx.showToast({
          icon: 'loading',
          title: res.data.msg,
        })
      }
    });

  },
  //点击选中
  chose_opt(e) {
    var index = e.currentTarget.dataset.index;
    var carriage = this.data.carriage;
    carriage[index].ischeck = !carriage[index].ischeck
    this.setData({
      carriage: carriage,
    })
  },
  //添加熟人点击选中
  chose_acquaintance(e) {
    var index = e.currentTarget.dataset.index;
    var acquaintance = this.data.acquaintance;   //熟人列表
    var name = acquaintance[index].name;   //熟人姓名
    var phone = acquaintance[index].phone; //熟人电话
    acquaintance[index].status = !acquaintance[index].status;      //选中未选中
    var choseinfo = { name: name, phone: phone };   //将熟人
    console.log(acquaintance[index].status)
    var carriage = this.data.carriage;    //承运人列表
    //当选中的时候添加到承运人数组
    if (acquaintance[index].status == true) {
      carriage.push(choseinfo)   //添加到数组
    } else {
      for (var i = 0; i < carriage.length; i++) {
        if (phone == carriage[i].phone) {
          carriage.splice(i, 1)
        }
      }
    }
    this.setData({
      acquaintance: acquaintance,     //选中的状态
      carriage: carriage   //承运人列表重排
    })
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    var isel = options.isel;
    if(isel){
      this.setData({
        issel:isel
      })
    }
    
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    this.initCarrierList();
    
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {
    var currentpage = this.data.currentPage;
    var totalpage = this.data.totalPage;
    if (currentpage < totalpage) {
      this.setData({
        currentPage: currentpage + 1
      })
      this.initCarrierList();
    } else {
      console.log("已经是全部数据")
      // wx.showToast({
      //   'icon':'loading',
      //   title: '已经是全部数据了',
      // })
    }
  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})